package servlet;

import pojo.Categories;
import pojo.Food;
import service.CategoriesManager;
import service.FoodManager;
import service.factory.ManagerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ShowFoodListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CategoriesManager categoriesManager;
	private FoodManager foodManager;

	public ShowFoodListServlet() {
		super();
		categoriesManager = ManagerFactory.getCategoriesManagerInstance();
		foodManager = ManagerFactory.getFoodManagerrInstance();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String typeId = request.getParameter("typeId");
		String m_id = request.getParameter("m_id");
		String m_name = request.getParameter("m_name");
		if (m_id == null)
			m_id = (String) request.getAttribute("m_id");

		List<Categories> categoriesList = categoriesManager.findAll();
		categoriesList.add(0, new Categories("all", "all"));
		List<Food> foodList = null;
		String typeName = null;
		if (typeId == null || "all".equals(typeId)) {
			typeId = "all";
			typeName = "all";
		} 
		else{
			typeName=categoriesManager.load(typeId).getType();
		}

		foodList = foodManager.find(m_id, typeId);

		request.setAttribute("m_name", m_name);
		request.setAttribute("m_id", m_id);
		request.setAttribute("typeName", typeName);
		request.setAttribute("typeId", typeId);
		request.setAttribute("categoriesList", categoriesList);
		request.setAttribute("foodList", foodList);
		request.getRequestDispatcher("showFoodList.jsp").forward(request, response);
	}
}
